<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*"%>
<%@ page import="message.*"%>
<%@ page import="social.*"%>
<%@ page import="QuizPackage.*" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Profile</title>
</head>
<body>

<div id="container"><!-- Header Div -->
<div id="header" style="background-color: #3B5998; color: white; font: Tahoma; width:100%;float: center ">
<h1 style="margin-bottom: 0; text-align: center;"><!--User welcome message--> <%

	String username = (String) session.getAttribute("username");

	String profile = (String) request.getParameter("profileName");
	String profileName;
	if (profile == null) {
		profileName = (String) session.getAttribute("profileName");
	} else {
		profileName = profile;
	}
	
	if (username == null || profileName == null) {
		RequestDispatcher dispatch = request.getRequestDispatcher("homepage.jsp");
		dispatch.forward(request, response);
		return;
	}
	
 	SocialDBC db = (SocialDBC) request.getServletContext()
 			.getAttribute("database");
 	QuizDBC qdb = (QuizDBC) request.getServletContext().getAttribute(
 			"quizDatabase");

 	//System.out.println(String.format("username:%s profileName:%s",
 			//username, profileName));

 	out.println("<img src=\"Nuvola_apps_korganizer.png\" alt=\"Smiley face\" height=\"45\" width=\"45\" />");
 	if (profileName.equals(username)) {
 		out.println(String.format(
 				"<style=\"text-align: center\"> Welcome %s!",
 				profileName));
 	} else {
 		out.println(String.format(
 				"<style=\"text-align: center\"> %s's Profile </br>",
 				profileName));
 	}
 %> 
 
 </h1>
</div>

 <!-- Search Box -->
<div id="header" style="background-color: #3B5998; color: white; font: Tahoma ">
<form method="post" action="SearchServlet"><input type="text"
	name="queryUser" /> <input type="submit" value="Search" /></form>
</div>

<div id="buffer"style="background-color: #3B5998; width: 100%; height: 10px; float: left">
</div>

<div id="buttonContainer" style="background-color: #3B5998; color: white; font: Tahoma ">
<%
	//Log out button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 6%; float: left\">");
	out.println("<form action=\"LogoutServlet\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Log out\"/> </form>");
	out.println("</div>");

	//Log out button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 8%; float: left\">");
	out.println("<form action=\"CreateQuiz.jsp\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Create Quiz\"/> </form>");
	out.println("</div>");

	//Quiz List button
	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 7%; float: left\">");
	out.println("<form action=\"quizList.jsp\" method=\"post\">"
			+ "<input type=\"submit\" value=\"Quiz List\"/> </form>");
	out.println("</div>");

	out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 7%; float: left\">");
	out.println("<form method = \"post\" action = \"mailbox.jsp\">");
	out.println("<input type = \"submit\" value =\"Mailbox\"/>");
	out.println("</form>");
	out.println("</div>");

	//<!-- Mailbox Link --> 
	if (profileName.equals(username)) {

		if (db.checkAdmin(username)) {
			//Back to profile button
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
			out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
					+ username + "\">");
			out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
			out.println("</form>");
			out.println("</div>");
		} else {
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 72%; float: left\">");
			out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
					+ username + "\">");
			out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
			out.println("</form>");
			out.println("</div>");
		}

	} else {
		//Back to profile button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println("<form method = \"post\" action = \"user-profile.jsp?profileName="
				+ username + "\">");
		out.println("<input type = \"submit\" value =\"Back to Profile\"/>");
		out.println("</form>");
		out.println("</div>");
	}
%>

<%
	if (db.checkAdmin(username) && !profileName.equals(username)) {
		//Remove User Button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println(String
				.format("<form action=\"AdminServlet\" method=\"post\">"
						+ "<input type=\"submit\" value=\"Remove User\">"
						+ "<input name=\"action\" type=\"hidden\" value=\"removeUser\"/>"
						+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
						+ "</form>", profileName));
		out.println("</div>");

		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 52%; float: left\">");
		if (!db.checkAdmin(profileName)) {
			//Promote to Admin button
			out.println(String
					.format("<form action=\"AdminServlet\" method=\"post\">"
							+ "<input type=\"submit\" value=\"Promote to Admin\">"
							+ "<input name=\"action\" type=\"hidden\" value=\"promoteAdmin\"/>"
							+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
							+ "</form>", profileName));
			out.println("</div>");
		} else {

			//Remove Admin button
			out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 59%; float: left\">");
			out.println(String
					.format("<form action=\"AdminServlet\" method=\"post\" float: right>"
							+ "<input type=\"submit\" value=\"Remove Admin\">"
							+ "<input name=\"action\" type=\"hidden\" value=\"removeAdmin\"/>"
							+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
							+ "</form>", profileName));
			out.println("</div>");
		}
		//out.println("</div>");
	}
	if (db.checkAdmin(username) && (profileName.equals(username))) {
		//Site Statistics button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 10%; float: left\">");
		out.println(String
				.format("<form action=\"statistics.jsp\" method=\"post\" float: right>"
						+ "<input type=\"submit\" value=\"Site Statistics\">"
						+ "</form>"));
		out.println("</div>");

		//Make announcement button
		out.println("<div id=\"button\"style=\"background-color: #3B5998; width: 52%; float: left\">");
		out.println(String
				.format("<form action=\"create-announcement.jsp\" method=\"post\" float: right>"
						+ "<input type=\"submit\" value=\"Make Announcement\">"
						+ "<input name=\"userAction\" type=\"hidden\" value=\"%s\"/>"
						+ "</form>", profileName));
		out.println("</div>");
	}
%>

</div>

<div id="buffer"style="background-color: #3B5998; width: 100%; height: 10px; float: left">
</div>
<!--  END OF HEADER -->

<div id="Friends"
	style="background-color: #FFFFFF; min-height: 350px; max-height: 350px; width: 20%; float: left; color: #3B5998;">
<!-- Friend Request Link --> <%
 	if (!profileName.equals(username)) {
 		//someone else's page
 		if (!db.alreadyFriends(username, profileName)) {
 			//not already friends
 			if (db.friendRequestSent(profileName, username)) {
 				//friend request sent but not accepted by other user
 				out.println("<button type=\"button\" disabled=\"disabled\">Friend Request Sent</button>");
 			} else if (db.friendRequestSent(username, profileName)) {
 				//friend request to user but not accepted
 				out.println(String
 						.format("<form action=\"RequestServlet\" method=\"post\">"
 								+ "<input type=\"submit\" value=\"Accept Friend Request\">"
 								+ "<input name=\"action\" type=\"hidden\" value=\"accept\"/>"
 								+ "<input name=\"toUser\" type=\"hidden\" value=\"%s\"/>"
 								+ "<input name=\"fromUser\" type=\"hidden\" value=\"%s\"/>"
 								+ "</form>", profileName, username));
 			} else {
 				//friend request not sent at all
 				out.println(String
 						.format("<form action=\"RequestServlet\" method=\"post\">"
 								+ "<input type=\"submit\" value=\"Send Friend Request\">"
 								+ "<input name=\"action\" type=\"hidden\" value=\"request\"/>"
 								+ "<input name=\"toUser\" type=\"hidden\" value=\"%s\"/>"
 								+ "<input name=\"fromUser\" type=\"hidden\" value=\"%s\"/>"
 								+ "</form>", profileName, username));
 			}
 		} else {
 			//remove friend if already friends
 			out.println(String
 					.format("<form action=\"RequestServlet\" method=\"post\">"
 							+ "<input type=\"submit\" value=\"Remove Friend\">"
 							+ "<input name=\"action\" type=\"hidden\" value=\"remove\"/>"
 							+ "<input name=\"toUser\" type=\"hidden\" value=\"%s\"/>"
 							+ "<input name=\"fromUser\" type=\"hidden\" value=\"%s\"/>"
 							+ "</form>", profileName, username));
 		}
 	}
 %> 
 
<!-- Friend List --> <%
 	if (username.equals(profileName)
 			|| db.alreadyFriends(profileName, username)) {
 		out.println(String
 				.format("<b><a href=\"friendList.jsp?profileName=%s\" > Friend List</a></b>",
 						profileName));
 		out.println("<img src=\"my_friends_icon.jpg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
 		ArrayList<String> friendList = db.getFriendList(profileName);
 		out.println("<p></p>");
 		for (int i = 0; i < friendList.size(); i++) {
 			String friendName = friendList.get(i);
 			out.println(String
 					.format("<a href=\"user-profile.jsp?profileName=%s\" > %s </a><br/>",
 							friendName, friendName));
 		}
 	} else {
 		out.println("<b>Friend List</b>");
 		out.println("<img src=\"my_friends_icon.jpg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
 		out.println("<p></p>");
 	}
 %>
</div>


<div id="message" style="background-color: #EEEEEE; min-height: 350px; max-height: 350px; width: 20%; float: left; color: #3B5998">
<%
	if (username.equals(profileName)) {
		out.println(String
				.format("<b><a href=\"mailbox.jsp?profileName=%s\" > Mail Activities</a></b>",
						profileName));
		out.println("<img src=\"mail_logo.jpg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
		if (profileName.equals(username)) {
			out.println("<p><u>Unread messages:</u>");
			out.println(" " + db.getNumUnreadMail(username));
			out.println("<p><u>Recent messages:</u>");

			ArrayList<Integer> mailIdList = db.getMailIdList(username);
			int show = 3;
			if (mailIdList.size() < show)
				show = mailIdList.size();
			for (int i = mailIdList.size() - 1; i >= mailIdList.size()
					- show; i--) {
				int id = mailIdList.get(i);
				AbstractMessage message = db.getMailById(id);
				String fromUser = message.getFromUser();
				String toUser = message.getToUser();
				String type = message.getType();

				if (type == "note")
					type = "Note";
				if (type == "request")
					type = "Friend Request";
				if (type == "challenge")
					type = "Quiz Challenge";

				out.println(String.format("<p> *%s ", type));
				out.println(String
						.format(" from <a href=\"user-profile.jsp?profileName=%s\" > %s </a>",
								fromUser, fromUser));
				out.println(String
						.format(" to <a href=\"user-profile.jsp?profileName=%s\" > %s </a>",
								toUser, toUser));
				if (type == "Note") {
					if (db.checkMailViewed(id)) {
						//message already viewed
						out.println(String
								.format("<a href=show-message.jsp?id=%d> (Read) </a></p>",
										id));
					} else {
						//message unread
						out.println(String
								.format("<a href=show-message.jsp?id=%d><b> (Unread) </b></a></p>",
										id));
					}
				}
			}
		}
	} else {
		out.println("<b>Mail Activities</b>");
		out.println("<img src=\"mail_logo.jpg\" alt=\"Smiley face\" height=\"30\" width=\"30\" />");
		out.println("<p></p>");
	}
%>
</div>


<div id=QuizHistory
	style="background-color: #FFFFFF; min-height: 350px; max-height: 350px; width: 30%; float: left; color: #3B5998;">

<%
	if (username.equals(profileName)
			|| db.alreadyFriends(profileName, username)) {
		out.println(String
				.format("<b><a href=\"quizHistory.jsp?profileName=%s\" > Quiz History</a></b>",
						profileName));
		out.println("<img src=\"paper_logo.jpg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
		out.println("<p></p>");
		ArrayList<Integer> createdList = qdb
				.getQuizListByCreator(profileName);
		if (createdList.size() == 0) {
			out.println("<p>No quizzes created.</p>");
		} else {
			out.println("<u> Recently created quizzes</u></br>");
			for (int i = 0; i < 5; i++) {
				if (i == createdList.size()) {
					break;
				} else {
					int quizID = createdList.get(i);
					String quizName = qdb.getStringByID(quizID, "name");
					out.println(String
							.format("<a href=\"StartQuiz?quizid=%d\" > %s </a><br/>",
									quizID, quizName));
				}

			}
		}
		out.println("<p></p>");

		ArrayList<Integer> takenList = qdb.getResults(profileName);
		if (takenList.size() == 0) {
			out.println("<p>No quizzes taken.</p>");
		} else {
			out.println("<u> Recently taken quizzes </u></br>");
			for (int i = 0; i < 5; i++) {
				if (i == takenList.size()) {
					break;
				} else {
					int quizID = takenList.get(i);
					String quizName = qdb.getStringByID(quizID, "name");
					out.println(String
							.format("<a href=\"StartQuiz?quizid=%d\" > %s </a><br/>",
									quizID, quizName));
				}
			}
		}
	} else {
		out.println("<b>Quiz History</b>");
		out.println("<img src=\"paper_logo.jpg\" alt=\"Smiley face\" height=\"30\" width=\"30\" />");
		out.println("<p></p>");
	}
%>
</div>

<div id=Announcement
	style="background-color: #EEEEEE; height: 350px; max-height: 350px; width: 30%; float: left; color: #3B5998">
<%
	out.println("<b><a href=\"announcement.jsp\" > Announcement</a></b>");
	out.println("<img src=\"speaker_logo.jpg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
	ArrayList<String> announcement = db.getAnnouncement();
	int index = 8;
	if (announcement.size() < index)
		index = announcement.size();
	for (int i = announcement.size() - index; i < announcement.size(); i++) {
		out.println("<p>" + announcement.get(i) + "</p>");
	}
%>
</div>

<div id="buffer"style="background-color: #3B5998; width: 100%; height: 10px; float: left">
</div>

<div id=RecentQuizzes
	style="background-color: #EEEEEE; min-height: 350px; max-height: 450px; width: 20%; float: left; color: #3B5998">
<b style = "text-align: center">Recently Created Quizzes</b>
<img src="Treasure logo.jpg" alt="Smiley face" height="35" width="35" >
<br>
<%
 	ArrayList<Integer> quizList = qdb.getQuizList(false);
 	if (quizList.size() == 0) {
 		out.println("<p>No quizzes created.</p>");
 	} else {
 		out.println("</br>");
 		for (int i = 0; i < 10; i++) {
 			if (i == quizList.size()) {
 				break;
 			} else {
 				int quizID = quizList.get(i);
 				String quizName = qdb.getStringByID(quizID, "name");
 				out.println(String
 						.format("<a href=\"StartQuiz?quizid=%d\" > %s </a><br/>",
 								quizID, quizName));
 			}
 		}
 	}
 %>
</div>

<div id=PopularQuizzes
	style="background-color: #FFFFFF; min-height: 350px; max-height: 450px; width: 20%; float: left; color: #3B5998">
<b style = "text-align: center"> Popular Quizzes</b>
<img src="Diamond PV Logo.jpg" alt="Smiley face" height="35" width="35" >
<br>
<br>
<%
	ArrayList<Integer> popularQuiz = qdb.getMostPopular();
	int temp = 10;
	if (temp > popularQuiz.size()) temp = popularQuiz.size();
	for (int i = popularQuiz.size()-1; i>=popularQuiz.size()-temp; i--) {
		out.println(String
				.format("<a href=\"StartQuiz?quizid=%d\" > %s </a></br>",
						popularQuiz.get(i), qdb.getStringByID(popularQuiz.get(i), "name")));
	}
%>
</div>

<div id=FriendActivities
	style="background-color: #EEEEEE; min-height: 350px; max-height: 450px; width: 30%; float: left; color: #3B5998">
<%
	if (username.equals(profileName)) {
		out.println(String
				.format("<b><a href=\"friend-activity.jsp?profileName=%s\" > Friend Recent Activities</a></b>",
						profileName));
		out.println("<img src=\"fb-friend-logo-2.jpeg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
		if (profileName.equals(username)) {
			ArrayList<String> friends = db.getFriendList(username);
			int num = 3;
			if (num > friends.size()) num = friends.size();
			out.println("<ul>");
			for (int i = 0; i < num; i++) {
				String name = friends.get(i);
				out.println("<p>");
				out.println(String
						.format("<a href=\"user-profile.jsp?profileName=%s\" > <b>%s</b> </a>:<br/>",
								name, name));
				out.println("Recent quizzes taken: ");

				if (username.equals(profileName)) {

					ArrayList<Integer> takenList = qdb.getResults(name);
					if (takenList.size() == 0) {
						out.println("No quizzes taken.");
					} else {
						for (int j = 0; j < 2; j++) {
							if (j == takenList.size()) {
								break;
							} else {
								int quizID = takenList.get(j);
								String quizName = qdb.getStringByID(
										quizID, "name");
								out.println(String
										.format("<a href=\"StartQuiz?quizid=%d\" > %s </a>, ",
												quizID, quizName));
							}
						}
					}
				}

				out.println("</br>");
				if (username.equals(profileName)) {
					ArrayList<Integer> createdList = qdb
							.getQuizListByCreator(name);
					if (createdList.size() == 0) {
						out.println("No quizzes created.");
					} else {
						out.println("Recently created quizzes</br>");
						for (int j = 0; j < 2; j++) {
							if (j == createdList.size()) {
								break;
							} else {
								int quizID = createdList.get(j);
								String quizName = qdb.getStringByID(
										quizID, "name");
								out.println(String
										.format("<a href=\"StartQuiz?quizid=%d\" > %s </a>, ",
												quizID, quizName));
							}

						}
					}
				}
				out.println("</br>");
				out.println("Recent achievements earned: ");
				ArrayList<String> acheivementList = db.getAcheivementList(name);
				if (acheivementList.size()==0) {
					out.println("No acheivements earned.");
				} else {
					for (int j=0; j<acheivementList.size(); j++) {
						out.println(acheivementList.get(j));
						out.print(", ");
					}
				}
				out.println("</br>");
				out.println("</p>");
			}
			out.println("</ul>");
		}
	} else {
		out.println("<b>Friend Recent Activities</b>");
		out.println("<img src=\"fb-friend-logo-2.jpeg\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
		out.println("<p></p>");
	}
%>
</div>

<div id=Achievements
	style="background-color: #FFFFFF; min-height: 350px; max-height: 450px; width: 30%; float: left; color: #3B5998">
<b style = "text-align: center"> Achievements</b>
<%
	out.println("<img src=\"champion.gif\" alt=\"Smiley face\" height=\"35\" width=\"35\" />");
	out.println("<br>");
	ArrayList<String> acheivementList = db.getAcheivementList(profileName);
	if (acheivementList.size()==0) {
		out.println("No acheivements earned.");
	} else {
		out.println("<ul>");
		for (int i=0; i<acheivementList.size(); i++) {
			out.println("<li>"+acheivementList.get(i)+"</li>");
			out.println("</br>");
		}
		out.println("</ul>");
	}

%>
</div>
</div>
</body>
</html>